package com.xzc.tmp.test;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.Key;

/**
 * 3DES加密工具类
 */
public class DES3UtilIPTV {
    // 密钥
    private final static String SECRETKEY = "G7AXRTE536754V59\0\0\0\0\0\0\0\0";
//    private final static String SECRETKEY = "G7AXRTE536754V5900000000";
    // 向量
    private final static String IV = "3267hisc";
    // 加解密统一使用的编码方式
    private final static String encoding = "utf-8";

    /**
     * 3DES加密
     *
     * @param plainText 普通文本
     * @return
     */
    public static String encrypt(String plainText) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(SECRETKEY.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
        return Base64.encode(encryptData);
    }

    /**
     * 3DES解密
     *
     * @param encryptText 加密文本
     * @return
     */
    public static String decrypt(String encryptText) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(SECRETKEY.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);

        byte[] decryptData = cipher.doFinal(Base64.decode(encryptText));

        return new String(decryptData, encoding);
    }

    /**
     * 3DES加密
     *
     * @param secretKey 秘钥
     * @param iv        偏移向量
     * @param plainText 普通文本
     * @return
     * @throws Exception
     */
    public static String encryptString(String secretKey, String iv,
                                       String plainText) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
        return Base64.encode(encryptData);
    }

    /**
     * 3DES解密
     *
     * @param secretKey   秘钥
     * @param iv          偏移向量
     * @param encryptText 密文
     * @return
     * @throws Exception
     */
    public static String decryptString(String secretKey, String iv,
                                       String encryptText) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);

        byte[] decryptData = cipher.doFinal(Base64.decode(encryptText));

        return new String(decryptData, encoding);
    }

    /**
     * 3DES解码后解密
     *
     * @param secretKey   秘钥
     * @param iv          偏移向量
     * @param encryptText 密文
     * @return
     * @throws Exception
     */
    public static String decryptStringURLDecoder(String secretKey, String iv,
                                                 String encryptText) throws Exception {
        String retJsonStr = decryptString(secretKey, iv,
                URLDecoder.decode(encryptText));
        return retJsonStr;
    }

    /**
     * URLEncoder编码加密信息
     *
     * @param secretKey
     * @param iv
     * @param plainText
     * @return
     * @throws Exception
     */
    public static String encryptStringURLEncoder(String secretKey, String iv,
                                                 String plainText) throws Exception {
        String base64Str = encryptString(secretKey, iv, plainText);
        return URLEncoder.encode(base64Str);
    }

    public static void main(String[] args) throws Exception {
//		String content = "8duiOJurUHvy8f6pANhmDTOK4hPTdyYXeGG1sb4FsHxBhmVVGSVmIXDh9uVF+T%2FRZgl3IN4XDcz5igGDPsSIqkvNe%2B%2FCdHg3fDEl%2BWtYZ1mIXAAHDT9ZTlfW5+djyJh8ertW%2FNU6CaY8w78f0X0ZotwoxZj0zHct0MXmDB9D37WDMx0cMnn%2BsD+ON1rLKLfrtPKGjlbFkLKVCSaqnKIQlUKLCM2CNwSr%2FUd%2BvJ79PvH3dA%3D";
//		content = URLDecoder.decode( content,"UTF-8" );
//		System.out.println(decrypt(content));

        String json = "{\"Naccount\":\"iptv7710069982@iptv.gx\",\"account\":\"iptv7710069982\",\"Model\":\"E900-s\",\"Mac\":\"88-51-FB-FE-D8-48\",\"Version\":\"1.0.5\",\"faultId\":\"127.0.0.1\",\"company\":\"\"}";
//        String json = "{\"Naccount\":\"iptv7718902085@iptv.gx\",\"account\":\"iptv7718902085\",\"Model\":\"TY1208-Z\",\"Mac\":\"78:b8:4b:07:79:22\",\"Version\":\"TY_OTT_H1.00.05\",\"faultId\":\"10010\",\"company\":\"SCTY\"}";
//        String json = "{\"Naccount\":\"iptv7718902085@iptv.gx\",\"account\":\"iptv7718902085\",\"Model\":\"TY1208-Z\",\"Mac\":\"78:b8:4b:07:79:22\",\"Version\":\"TY_OTT_H1.00.05\",\"faultId\":\"10010\",\"company\":\"SCTY\"}";
        String encryptStr = encrypt(json);
        String urlJson = URLEncoder.encode(encryptStr, "UTF-8");
        System.out.println(urlJson);
    }
}